/**
* Copyright (C) 2021 All rights reserved.
*
* @file main2.h
* @author Wang Haibin
* @email wanghaibin@qiyi.com
* @date Wed Dec 20 10:20:43 CST 2023
* @brief 将ip转为二进制字符串
*/
#include <iostream>
#include <sstream>
#include <bitset>
#include <string>

std::string convertIPtoBinary(const std::string& ip) {
    std::istringstream iss(ip);
    std::string segment;
    std::string binaryIP;

    while(std::getline(iss, segment, '.')) {
        int num = std::stoi(segment);
        std::bitset<8> bin(num);
        binaryIP += bin.to_string();
        binaryIP += ",";
    }

    return binaryIP;
}

int main() {
    std::string ip = "192.168.0.1";
    std::string binaryIP = convertIPtoBinary(ip);
    std::cout << ip << "=" << binaryIP << std::endl;
    return 0;
}
